using System;
using Godot;

namespace Arcane.Source.Combat
{
    public abstract class BaseUltraSpell
    {
        public event Action<float> OnEnergyChange;

        private readonly int _maxEnergy;
        public int MaxEnergy => _maxEnergy;
        private float _energy;
        public float Energy
        {
            get => _energy;
            set
            {
                _energy = Mathf.Clamp(value, 0, MaxEnergy);
                OnEnergyChange?.Invoke(_energy);
            }
        }
        public bool AutoEnabled { get; set; }

        private Entity _carrier;
        public Entity Carrier => _carrier;

        public BaseUltraSpell(int maxEnergy, int initialEnergy)
        {
            _maxEnergy = maxEnergy;
            _energy = initialEnergy;
        }

        public virtual void Bind(Entity entity)
        {
            _carrier = entity;
        }

        public abstract void OnCast();

        public abstract bool CanCast();

        public void Cast()
        {
            if (!CanCast()) return;

            Energy = 0;
            OnCast();
        }

        public void Tick()
        {
            if (!AutoEnabled) return;
            Cast();
        }
    }
}